package com.ojoin.ojoin.eb_spider.service.stat;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.ojoin.ojoin.eb_spider.utils.WebServiceUtils;
import com.ojoin.trade.common.utils.CommonUtils;
import com.ojoin.trade.common.utils.LogUtils;
import com.ojoin.trade.common.utils.TimeUtils;
import com.ojoin.trade.supplyloan_core.web.domain.TbCatchdataStrategy;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlLoanStatTotal;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlPlatformUser;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlSupplyInventory;
import com.ojoin.trade.supplyloan_core.web.domain.TbSlSupplyPayment;
@Service
public class ThirdSellerSUNINGstat  extends BaseStatTotal{

	private Logger log = LoggerFactory.getLogger(ThirdSellerSUNINGstat.class);
	
	@Override
	public void invoke(TbSlPlatformUser user, TbCatchdataStrategy ca,TbSlLoanStatTotal total) throws Exception {

		StringBuffer sb = new StringBuffer();
		
		String stime = WebServiceUtils.getPaymentLastsettlementDate(user.getId())
				.replace("/", "-");

		if (CommonUtils.isEmpty(stime)) {
			stime = WebServiceUtils.getPaymentLastInsertTime( user.getId());
		}
		String etime = TimeUtils.getCurDate();

		long inveInMoney = WebServiceUtils.getSupplyInventoryMoneyByplateformId(user.getId(),ca, stime, etime, TbSlSupplyInventory.IN);
		
		long inveOutMoney = WebServiceUtils.getSupplyInventoryMoneyByplateformId(user.getId(),ca, stime, etime, TbSlSupplyInventory.OUT);

		

		total.setTotalUnsettleInstoreorder(Long.valueOf(inveInMoney));
		total.setTotalUnenterinRefundorder(Long.valueOf(inveOutMoney));

		Set<String> waitpayids = new HashSet<String>();
		
		String pstime = TimeUtils.getDateBefore(ca.getSettlementInterval());

		List<TbSlSupplyPayment>  waitpays =  WebServiceUtils.getSupplyPaymentByplateformId(user.getId(), ca,pstime, etime, TbSlSupplyPayment.WAITPAY);
		

		List<TbSlSupplyPayment>  completepays = WebServiceUtils.getSupplyPaymentByplateformId(user.getId(),ca, pstime, etime, TbSlSupplyPayment.COMPLETEPAY);
		
		

		if (null != waitpays) {
			Iterator<TbSlSupplyPayment> it = waitpays.iterator();
			while (it.hasNext()) {
				TbSlSupplyPayment tbsl = it.next();
				
				waitpayids.add(tbsl.getSettlementId());
				sb.append("[waitpayid:" + tbsl.getSettlementId() + "---unpay:"
						+ tbsl.getAmount() + "]");
				total.setTotalUnpaySettleorder(total.getTotalUnpaySettleorder()
						+ tbsl.getAmount());

			}

		}

		if (null != completepays) {
			Iterator<TbSlSupplyPayment>  it = completepays.iterator();
			while (it.hasNext()) {

				TbSlSupplyPayment tbsl = it.next();
				if (waitpayids.contains(tbsl.getSettlementId())) {
					sb.append("[payid:" + tbsl.getPayId() + "---pay:"
							+ tbsl.getAmount() + "]");
					total.setTotalPaySettleorder(total.getTotalPaySettleorder()
							+ tbsl.getAmount());
				}
			}
		}

		LogUtils.info(
				log,
				" %s --> [ store in stime:%s--etime:%s] + [store out stime:%s--etime:%s] + [ Unpay  %s] ",
				user.getUsername(), stime, etime, stime, etime, sb);

	}

}
